package de.teamgamma.cansat.groundstation.options;


import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.text.ParseException;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import javax.swing.text.MaskFormatter;

import de.szut.gui.language.Languages;
import de.szut.gui.language.TranslatableComponent;
import de.szut.gui.language.TranslatableFileChooser;
import de.szut.gui.language.TranslatableLabel;
import de.szut.gui.options.Options;
import de.szut.gui.options.OptionsCheckBox;
import de.szut.gui.options.OptionsComboBox;
import de.szut.gui.options.OptionsComponent;
import de.szut.gui.options.OptionsFormattedTextField;
import de.szut.gui.options.OptionsTextArea;
import de.teamgamma.cansat.groundstation.control.Controller;
import de.teamgamma.cansat.groundstation.control.GUIController;
import de.teamgamma.cansat.groundstation.main.DefaultPanel;
import de.teamgamma.cansat.groundstation.main.MainMenuPanel;

/**
 * Panel for the options
 * @author Marc Huisinga
 *
 */

public class OptionPanel extends DefaultPanel {

	private static final long serialVersionUID = 3111510925736940009L;

	/**
	 * X-Position where the generation of option components is started
	 */
	public static final int STARTPOSITIONX = 20;
	/**
	 * Y-Position where the generation of option components is started
	 */
	public static final int STARTPOSITIONY = 50;
	/**
	 * Default-Distance between option components
	 */
	public static final int DIST = 26;
	/**
	 * Default-Width of option components
	 */
	public static final int BUTTONWIDTH = 190;
	/**
	 * Default-Height of option components
	 */
	public static final int BUTTONHEIGHT = 23;
	
	private OptionsCheckBox chckbxStreamDataToSmartphone;
	private OptionsCheckBox chckbxStreamDataToWebsite;
	private OptionsCheckBox chckbxDebugMode;
	private OptionsCheckBox chckbxLogData;
	private TranslatableLabel lblLanguageInfo;
	private TranslatableLabel lblAPIKeyInfo;
	private TranslatableLabel lblInputLocationInfo;
	private OptionsComboBox cmbbxLanguage;
	private OptionsFormattedTextField fmtfAPIKey;
	private OptionsTextArea txtaInputLocation;
	
	private ArrayList<OptionsComponent> optionComponents;
	private ArrayList<TranslatableComponent> translatableComponents;
	
	public OptionPanel() {
		
		optionComponents = new ArrayList<OptionsComponent>();
		translatableComponents = new ArrayList<TranslatableComponent>();
		
		Languages.prepareComponent("options.OptionPanel.header", getHeader(), translatableComponents);
		
		Languages.prepareComponent("options.OptionPanel.return", getReturnButton(), translatableComponents);
		getReturnButton().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Controller.getInstance().getOptions().saveOptions(optionComponents);
				Controller.getInstance().getGuiController().getLanguages().setCurrentLanguage(Controller.getInstance().getOptions().getOptionValue(OptionKeys.LANGUAGE));
				Controller.getInstance().getGuiController().getLanguages().translateSpecificComponent(Controller.getInstance().getGuiController().getMainFrame());
				Controller.getInstance().getGuiController().toggleCurrentPanel(new MainMenuPanel());
			}		
		});
		
		int currentPosY = STARTPOSITIONY;
		
		chckbxDebugMode = new OptionsCheckBox();
		Languages.prepareComponent("options.OptionPanel.debug_mode", chckbxDebugMode, translatableComponents);
		chckbxDebugMode.setBounds(STARTPOSITIONX, currentPosY, BUTTONWIDTH+50, BUTTONHEIGHT);
		chckbxDebugMode.setFont(GUIController.COMPONENTFONT);
		Options.prepareComponent(OptionKeys.DEBUGMODE, chckbxDebugMode, optionComponents);
		add(chckbxDebugMode);
		
		currentPosY += DIST;
		
		chckbxLogData = new OptionsCheckBox();
		Languages.prepareComponent("options.OptionPanel.log_data", chckbxLogData, translatableComponents);
		chckbxLogData.setBounds(STARTPOSITIONX, currentPosY, BUTTONWIDTH+50, BUTTONHEIGHT);
		chckbxLogData.setFont(GUIController.COMPONENTFONT);
		Options.prepareComponent(OptionKeys.LOGDATA, chckbxLogData, optionComponents);
		add(chckbxLogData);
		
		currentPosY += DIST;
		
		chckbxStreamDataToSmartphone = new OptionsCheckBox();
		Languages.prepareComponent("options.OptionPanel.steam_data_to_smartphone", chckbxStreamDataToSmartphone, translatableComponents);
		chckbxStreamDataToSmartphone.setBounds(STARTPOSITIONX, currentPosY, BUTTONWIDTH+50, BUTTONHEIGHT);
		chckbxStreamDataToSmartphone.setFont(GUIController.COMPONENTFONT);
		Options.prepareComponent(OptionKeys.STREAMDATATOSMARTPHONE, chckbxStreamDataToSmartphone, optionComponents);
		add(chckbxStreamDataToSmartphone);
		
		currentPosY += DIST;
		
		chckbxStreamDataToWebsite = new OptionsCheckBox();
		Languages.prepareComponent("options.OptionPanel.steam_data_to_website", chckbxStreamDataToWebsite, translatableComponents);
		chckbxStreamDataToWebsite.setBounds(STARTPOSITIONX, currentPosY, BUTTONWIDTH+50, BUTTONHEIGHT);
		chckbxStreamDataToWebsite.setFont(GUIController.COMPONENTFONT);
		Options.prepareComponent(OptionKeys.STREAMDATATOWEBSITE, chckbxStreamDataToWebsite, optionComponents);
		add(chckbxStreamDataToWebsite);
		
		currentPosY += DIST;
		
		lblLanguageInfo = new TranslatableLabel();
		Languages.prepareComponent("options.OptionPanel.language_info", lblLanguageInfo, translatableComponents);
		lblLanguageInfo.setBounds(STARTPOSITIONX, currentPosY, BUTTONWIDTH, BUTTONHEIGHT);
		lblLanguageInfo.setFont(GUIController.COMPONENTFONT);
		add(lblLanguageInfo);
		
		currentPosY += 0.8*DIST;
		
		String[] cmbbxValues = new String[Controller.getInstance().getGuiController().getLanguages().getLanguages().size()];
		int index = 0;
		for (String currentLanguage : Controller.getInstance().getGuiController().getLanguages().getLanguages()) {
			cmbbxValues[index] = currentLanguage;
			index ++;
		}
		
		cmbbxLanguage = new OptionsComboBox(cmbbxValues);
		cmbbxLanguage.setBounds(STARTPOSITIONX, currentPosY, BUTTONWIDTH, BUTTONHEIGHT-5);
		cmbbxLanguage.setFont(GUIController.COMPONENTFONT);
		Options.prepareComponent(OptionKeys.LANGUAGE, cmbbxLanguage, optionComponents);
		add(cmbbxLanguage);
		
		currentPosY += DIST;
		
		lblAPIKeyInfo = new TranslatableLabel();
		Languages.prepareComponent("options.OptionPanel.apikey_info", lblAPIKeyInfo, translatableComponents);
		lblAPIKeyInfo.setBounds(STARTPOSITIONX, currentPosY, BUTTONWIDTH, BUTTONHEIGHT);
		lblAPIKeyInfo.setFont(GUIController.COMPONENTFONT);
		add(lblAPIKeyInfo);
		
		currentPosY += 0.8*DIST;
		
		try {
			MaskFormatter formatter = new MaskFormatter("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
			formatter.setPlaceholderCharacter('0');
			fmtfAPIKey = new OptionsFormattedTextField();
			fmtfAPIKey.setBounds(STARTPOSITIONX, currentPosY, 2*BUTTONWIDTH+10, BUTTONHEIGHT);
			fmtfAPIKey.setFont(new Font("Courier New", Font.PLAIN, 10));
			formatter.install(fmtfAPIKey);
			Options.prepareComponent(OptionKeys.APIKEY, fmtfAPIKey, optionComponents);
			add(fmtfAPIKey);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		currentPosY += DIST;
		
		lblInputLocationInfo = new TranslatableLabel();
		Languages.prepareComponent("options.OptionPanel.inputlocation_info", lblInputLocationInfo, translatableComponents);
		lblInputLocationInfo.setBounds(STARTPOSITIONX, currentPosY, 2*BUTTONWIDTH, BUTTONHEIGHT);
		lblInputLocationInfo.setFont(GUIController.COMPONENTFONT);
		add(lblInputLocationInfo);
		
		currentPosY += 0.8*DIST;
		
		txtaInputLocation = new OptionsTextArea();
		txtaInputLocation.setEditable(false);
		txtaInputLocation.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				TranslatableFileChooser chooser = new TranslatableFileChooser();
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				chooser.setTranslationKey("options.OptionPanel.inputlocationchooser");
				chooser.setCurrentDirectory(new File(txtaInputLocation.getText()));
				Controller.getInstance().getGuiController().getLanguages().translateSpecificComponent(chooser);
				int option = chooser.showSaveDialog(null);
				if (option == JFileChooser.APPROVE_OPTION) {
					if (chooser.getSelectedFile().getAbsolutePath().indexOf(".") == -1) {
						txtaInputLocation.setOptionComponentState(chooser.getSelectedFile().getAbsolutePath());
					} else {
						System.err.println("Needs to be a directory!");
					}
				}
			}
		});
		Options.prepareComponent(OptionKeys.EXPINLOCKEY, txtaInputLocation, optionComponents);
		JScrollPane scroll = new JScrollPane(txtaInputLocation);
		scroll.setBounds(STARTPOSITIONX, currentPosY, 2*BUTTONWIDTH+10, BUTTONHEIGHT);
		add(scroll);
		
		
		Controller.getInstance().getOptions().loadOptions(optionComponents);
		Controller.getInstance().getGuiController().getLanguages().translateListOfComponents(translatableComponents);
	}


}
	